//
//  MySubscribeSortView.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/29.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "MySubscribeSortCell.h"

#import "MySubscribeSortView.h"

@interface MySubscribeSortView ()
<UICollectionViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UIButton *closeBtn;

@property(nonatomic,strong)UICollectionView *collectionView;

@property (nonatomic, strong) UIButton *clearBtn;

@property (nonatomic, assign) CGSize       cellSize;
@property (nonatomic, assign) UIEdgeInsets collectionEdge;

@end

@implementation MySubscribeSortView

- (instancetype)initWithArray:(NSMutableArray <ProClassificationModel *>*)dataArray {
    if (self = [super init]) {
        self.backgroundColor = [UIColor whiteColor];
        self.dataArray = dataArray;
        
        [self layout];
    }
    return self;;
}

//- (void)removeAllSelectStatus {
//    
//    for (RecommandCategoryModel *model in self.dataArray) {
//        model.isSelect = NO;
//    }
//}

- (void)closeView {
    
    if (self.MySubscribeSortViewBlock) {
        self.MySubscribeSortViewBlock(MySubscribeSortViewActionType_close, -1);
    }
}

//- (void)setDataArray:(NSMutableArray<RecommandCategoryModel *> *)dataArray {
//
//    [self.collectionView reloadData];
//}

- (void)layout {
    
    UILabel *headTitle = [[UILabel alloc] init];
    headTitle.text = @"选择预约分类";
    headTitle.font = [UIFont systemFontOfSize:14];
    headTitle.textAlignment = NSTextAlignmentCenter;
    
    [self addSubview:headTitle];
    [self addSubview:self.collectionView];
    [self addSubview:self.closeBtn];
    
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-Bottom_SafeHeight);
        make.top.mas_equalTo(kStatusBarHeight + NavBar_Height);
    }];
    
    [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.collectionView.mas_top);
        make.right.mas_equalTo(0);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(44);
    }];
    
    [headTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.closeBtn);
        make.left.right.mas_equalTo(0);
    }];
    
    CGFloat width = ProductCategoryViewWidth;
    CGFloat cellWidth;
    CGFloat distance = 5;

    cellWidth = width/2 - 2*distance;

    _cellSize =  CGSizeMake(cellWidth, 30);
    _collectionEdge = UIEdgeInsetsMake(distance, distance, distance, distance);
    
    [self.collectionView reloadData];

}

#pragma mark delegate && dataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MySubscribeSortCell *cell;
    if (!cell) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MySubscribeSortCellID" forIndexPath:indexPath];

        [cell fillCellWithModel:self.dataArray[indexPath.row]];
    }
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView
                 layout:(UICollectionViewLayout *)collectionViewLayout
 sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return _cellSize;
}

//设置cell的位置
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView
                       layout:(UICollectionViewLayout *)collectionViewLayout
       insetForSectionAtIndex:(NSInteger)section
{
    return _collectionEdge;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

//    BOOL selected = self.dataArray[indexPath.row].isSelect;
//    [self removeAllSelectStatus];
//    self.dataArray[indexPath.row].isSelect = !selected;
//
//    [self.collectionView reloadData];
//
    NSLog(@"indexPath = %ld----%ld",indexPath.section,indexPath.row);
    NSInteger index = indexPath.row;
    NSLog(@"index = %ld",index);

    if (self.MySubscribeSortViewBlock) {
        self.MySubscribeSortViewBlock(MySubscribeSortViewActionType_select, index);
    }
}


- (UICollectionView *)collectionView {

    if (!_collectionView) {
        
        UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
//        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

        _collectionView  = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = UIColorHex(0xf1f1f1);
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;

        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerNib:[UINib nibWithNibName:@"MySubscribeSortCell" bundle:nil] forCellWithReuseIdentifier:@"MySubscribeSortCellID"];
    }
    return _collectionView;
}


- (UIButton *)closeBtn {
    if (!_closeBtn) {
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeBtn setImage:[UIImage imageNamed:@"shop_closed"] forState:UIControlStateNormal];
//        [_closeBtn setTitle:@"X" forState:UIControlStateNormal];
//        [_closeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_closeBtn addTarget:self action:@selector(closeView) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

@end
